using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Plugin.Circle;
using MicroMsg.Resource.@string;
using MicroMsg.Storage;
using MicroMsg.UI.UserContrl;
using MicroMsg.UI.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
namespace MicroMsg.UI.Page
{
	public class SetPostVisibilityPage : BasePage
	{
		private const string TAG = "SetPostVisibilityPage";
		private const int cNPageArgCnt = 2;
		private MMStandardUIEvent mDlgVisiChanged;
		private EventWatcher mWatchSnsGroup;
		private object mObjectWatcher = new object();
		private List<SnsTagInfo> mCurGroups = new List<SnsTagInfo>();
		internal Grid LayoutRoot;
		internal StackPanel panelGeneralSet;
		internal SnsPostVisiItem cbInGroup;
		internal SnsPostVisiItem cbPublic;
		internal SnsPostVisiItem cbPrivate;
		internal Border borderLineCGroup;
		internal StackPanel panelCustomGroups;
		internal StackPanel panelBtns;
		internal ClickableLayout btnEditGroup;
		internal ClickableLayout btnSetOutGroup;
		private bool _contentLoaded;
		public SetPostVisibilityPage()
		{
			this.InitializeComponent();
			base.add_Loaded(new RoutedEventHandler(this.Page_Loaded));
			base.add_Unloaded(new RoutedEventHandler(this.Page_Unloaded));
		}
		private void Page_Loaded(object sender, RoutedEventArgs e)
		{
			Log.smethod_5("SetPostVisibilityPage", "Page_Loaded");
		}
		private void Page_Unloaded(object sender, RoutedEventArgs e)
		{
			Log.smethod_5("SetPostVisibilityPage", "Page_Unloaded");
		}
		protected override void OnPageIn(object[] object_0)
		{
			Log.smethod_5("SetPostVisibilityPage", "OnPageIn");
			if (object_0 != null)
			{
				if (2 == object_0.Length)
				{
					this.mDlgVisiChanged = (object_0[0] as MMStandardUIEvent);
					this.registerEvent();
					this.initUI();
					this.setChoosen(object_0[1] as List<ulong>);
					return;
				}
			}
		}
		protected override void OnPageOut()
		{
			Log.smethod_5("SetPostVisibilityPage", "OnPageOut");
			this.unRegisterEvent();
			if (this.mDlgVisiChanged != null)
			{
				this.mDlgVisiChanged(this, this.getVisualRange());
			}
		}
		protected override void OnPageBack()
		{
			Log.smethod_5("SetPostVisibilityPage", "OnPageBack");
		}
		private void registerEvent()
		{
			if (this.mWatchSnsGroup == null)
			{
				this.mWatchSnsGroup = new EventWatcher(this.mObjectWatcher, null, new EventHandlerDelegate(this.OnSnsGroup));
				EventCenter.registerEventWatcher(EventConst.ON_STORAGE_SNSTAG_ADD, this.mWatchSnsGroup);
				EventCenter.registerEventWatcher(EventConst.ON_STORAGE_SNSTAG_DEL, this.mWatchSnsGroup);
				EventCenter.registerEventWatcher(EventConst.ON_STORAGE_SNSTAG_MODIFY, this.mWatchSnsGroup);
			}
		}
		private void unRegisterEvent()
		{
			if (this.mWatchSnsGroup != null)
			{
				EventCenter.removeEventWatcher(EventConst.ON_STORAGE_SNSTAG_ADD, this.mWatchSnsGroup);
				EventCenter.removeEventWatcher(EventConst.ON_STORAGE_SNSTAG_DEL, this.mWatchSnsGroup);
				EventCenter.removeEventWatcher(EventConst.ON_STORAGE_SNSTAG_MODIFY, this.mWatchSnsGroup);
				this.mWatchSnsGroup = null;
			}
		}
		private void OnSnsGroup(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs == null || evtArgs.mObject == null)
			{
				return;
			}
			switch (evtArgs.mEventID)
			{
			case EventConst.ON_STORAGE_SNSTAG_ADD:
			{
				List<SnsTagInfo> listObject = evtArgs.getListObject<SnsTagInfo>();
				this.addGroupList(listObject);
				return;
			}
			case EventConst.ON_STORAGE_SNSTAG_DEL:
			{
				List<SnsTagInfo> listObject2 = evtArgs.getListObject<SnsTagInfo>();
				this.removeGroupList(listObject2);
				return;
			}
			case EventConst.ON_STORAGE_SNSTAG_MODIFY:
			{
				List<SnsTagInfo> listObject3 = evtArgs.getListObject<SnsTagInfo>();
				this.modifyGroupList(listObject3);
				return;
			}
			default:
				return;
			}
		}
		private void cbGernerSet_CheckStageChanged(object sender, object args)
		{
			if ((bool)args)
			{
				this.uncheckdAllExcept(sender, this.panelGeneralSet);
				this.uncheckdAllExcept(sender, this.panelCustomGroups);
			}
		}
		private void cbCustomSet_CheckStageChanged(object sender, object args)
		{
			if ((bool)args)
			{
				this.uncheckdAllExcept(sender, this.panelGeneralSet);
				return;
			}
			if (this.getCustomSetCheckedCnt() == 0)
			{
				this.cbInGroup.IsChecked = true;
			}
		}
		private int getCustomSetCheckedCnt()
		{
			int num = 0;
			using (IEnumerator<UIElement> enumerator = this.panelCustomGroups.get_Children().GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					UIElement current = enumerator.get_Current();
					SnsPostVisiItem snsPostVisiItem = current as SnsPostVisiItem;
					if (snsPostVisiItem != null && snsPostVisiItem.IsChecked)
					{
						num++;
					}
				}
			}
			return num;
		}
		private void uncheckdAllExcept(object object_0, Panel panel)
		{
			if (panel == null)
			{
				return;
			}
			using (IEnumerator<UIElement> enumerator = panel.get_Children().GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					UIElement current = enumerator.get_Current();
					if (object_0 != current)
					{
						SnsPostVisiItem snsPostVisiItem = current as SnsPostVisiItem;
						if (snsPostVisiItem != null)
						{
							snsPostVisiItem.IsChecked = false;
						}
					}
				}
			}
		}
		private void btnEditGroup_Click(object sender, RoutedEventArgs e)
		{
			SnsEditGroupsPage.GotoThisPage();
		}
		private void btnSetOutGroup_Click(object sender, RoutedEventArgs e)
		{
			SnsSetOutGroupPage.GotoThisPage();
		}
		private void initUI()
		{
			SnsTagInfo mSnsGroup = new SnsTagInfo
			{
				nTagID = 1uL
			};
			this.cbInGroup.mSnsGroup = mSnsGroup;
			this.cbInGroup.mItemKind = SnsPostVisiItemKind.Kind_GeneralSet;
			this.cbInGroup.txtTitle.set_Text(strings.WC_Commit_SelectGroup_Friend);
			this.cbInGroup.txtDescription.set_Text(strings.WC_Commit_SelectGroup_Friend_Desc);
			this.cbPublic.txtTitle.set_Text(strings.WC_Commit_SelectGroup_Public);
			this.cbPublic.txtDescription.set_Text(strings.WC_Commit_SelectGroup_Public_Desc);
			SnsTagInfo mSnsGroup2 = new SnsTagInfo
			{
				nTagID = 0uL
			};
			this.cbPrivate.mItemKind = SnsPostVisiItemKind.Kind_GeneralSet;
			this.cbPrivate.mSnsGroup = mSnsGroup2;
			this.cbPrivate.txtTitle.set_Text(strings.WC_Commit_SelectGroup_Private);
			this.cbPrivate.txtDescription.set_Text(strings.WC_Commit_SelectGroup_Private_Desc);
			Circle.TryGetSnsGroups();
			List<SnsTagInfo> list = StorageMgr.snsTag.getList();
			this.addGroupList(list);
		}
		private void addGroupList(List<SnsTagInfo> listGroup)
		{
			if (listGroup == null)
			{
				return;
			}
			using (List<SnsTagInfo>.Enumerator enumerator = listGroup.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					SnsTagInfo current = enumerator.get_Current();
					if (current != null && SnsGroupMgr.IsCustomGroup(current))
					{
						int nIndex = -1;
						if (!this.isInCurGroupList(current, ref nIndex))
						{
							this.insertGroupToCurList(nIndex, current);
						}
					}
				}
			}
			this.refreshCGroupLine();
		}
		private void insertGroupToCurList(int nIndex, SnsTagInfo snsTagInfo_0)
		{
			if (nIndex >= 0 && snsTagInfo_0 != null)
			{
				this.mCurGroups.Insert(nIndex, snsTagInfo_0);
				SnsPostVisiItem snsPostVisiItem = new SnsPostVisiItem();
				snsPostVisiItem.mItemKind = SnsPostVisiItemKind.Kind_CustomGroup;
				snsPostVisiItem.mSnsGroup = snsTagInfo_0;
				snsPostVisiItem.CheckStageChanged += new MMStandardUIEvent(this.cbCustomSet_CheckStageChanged);
				this.panelCustomGroups.get_Children().Insert(nIndex, snsPostVisiItem);
				return;
			}
		}
		private void modifyGroupList(List<SnsTagInfo> listGroup)
		{
			if (listGroup == null)
			{
				return;
			}
			int num = 0;
			using (List<SnsTagInfo>.Enumerator enumerator = listGroup.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					SnsTagInfo current = enumerator.get_Current();
					if (this.isInCurGroupList(current, ref num))
					{
						this.mCurGroups.set_Item(num, current);
						SnsPostVisiItem snsPostVisiItem = this.panelCustomGroups.get_Children().get_Item(num) as SnsPostVisiItem;
						if (snsPostVisiItem != null)
						{
							snsPostVisiItem.mSnsGroup = current;
						}
					}
				}
			}
		}
		private void removeGroupList(List<SnsTagInfo> listGroup)
		{
			if (listGroup == null)
			{
				return;
			}
			int num = 0;
			using (List<SnsTagInfo>.Enumerator enumerator = listGroup.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					SnsTagInfo current = enumerator.get_Current();
					if (this.isInCurGroupList(current, ref num))
					{
						this.mCurGroups.RemoveAt(num);
						this.panelCustomGroups.get_Children().RemoveAt(num);
					}
				}
			}
			this.refreshCGroupLine();
		}
		private bool isInCurGroupList(SnsTagInfo group, ref int nIndex)
		{
			nIndex = 0;
			using (List<SnsTagInfo>.Enumerator enumerator = this.mCurGroups.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					SnsTagInfo current = enumerator.get_Current();
					int num = this.compSnsTag(group, current);
					if (num == 0)
					{
						bool result = true;
						return result;
					}
					if (num < 0)
					{
						bool result = false;
						return result;
					}
					nIndex++;
				}
			}
			return false;
		}
		private int compSnsTag(SnsTagInfo snsTagInfo_0, SnsTagInfo snsTagInfo_1)
		{
			if (snsTagInfo_0 == null)
			{
				return -1;
			}
			if (snsTagInfo_1 == null)
			{
				return 1;
			}
			if (snsTagInfo_0.nTagID == snsTagInfo_1.nTagID)
			{
				return 0;
			}
			if (snsTagInfo_0.nTagID < snsTagInfo_1.nTagID)
			{
				return -1;
			}
			return 1;
		}
		private void refreshCGroupLine()
		{
			if (this.panelCustomGroups.get_Children().get_Count() == 0)
			{
				this.borderLineCGroup.set_Visibility(1);
				return;
			}
			this.borderLineCGroup.set_Visibility(0);
		}
		protected override void OnBackKeyPress(CancelEventArgs cancelEventArgs_0)
		{
		}
		public static void GotoThisPage(MMStandardUIEvent mDlgVisiChanged, List<ulong> listSnsGroup)
		{
			object[] object_ = new object[]
			{
				mDlgVisiChanged,
				listSnsGroup
			};
			ToolFunction.GotoPageForUserControl("/Source/Plugin/Sns/Common/UI/UploadAndPost/SetPostVisibilityPage.xaml", object_, false);
		}
		private List<ulong> getVisualRange()
		{
			List<ulong> result;
			using (IEnumerator<UIElement> enumerator = this.panelGeneralSet.get_Children().GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					UIElement current = enumerator.get_Current();
					SnsPostVisiItem snsPostVisiItem = current as SnsPostVisiItem;
					if (snsPostVisiItem != null && snsPostVisiItem.IsChecked)
					{
						if (snsPostVisiItem.mSnsGroup == null)
						{
							result = null;
							return result;
						}
						List<ulong> list = new List<ulong>();
						list.Add(snsPostVisiItem.mSnsGroup.nTagID);
						result = list;
						return result;
					}
				}
				goto IL_6F;
			}
			return result;
			IL_6F:
			List<ulong> list2 = new List<ulong>();
			using (IEnumerator<UIElement> enumerator2 = this.panelCustomGroups.get_Children().GetEnumerator())
			{
				while (enumerator2.MoveNext())
				{
					UIElement current2 = enumerator2.get_Current();
					SnsPostVisiItem snsPostVisiItem2 = current2 as SnsPostVisiItem;
					if (snsPostVisiItem2 != null && snsPostVisiItem2.IsChecked && snsPostVisiItem2.mSnsGroup != null)
					{
						list2.Add(snsPostVisiItem2.mSnsGroup.nTagID);
					}
				}
			}
			if (list2.get_Count() != 0)
			{
				return list2;
			}
			return null;
		}
		private void setChoosen(List<ulong> listGroupIds)
		{
			if (listGroupIds == null || listGroupIds.get_Count() == 0)
			{
				this.cbPublic.IsChecked = true;
				this.uncheckdAllExcept(this.cbPublic, this.panelGeneralSet);
				this.uncheckdAllExcept(this.cbPublic, this.panelCustomGroups);
				return;
			}
			if (listGroupIds.get_Item(0) == 1uL)
			{
				this.cbInGroup.IsChecked = true;
				this.uncheckdAllExcept(this.cbInGroup, this.panelGeneralSet);
				this.uncheckdAllExcept(this.cbInGroup, this.panelCustomGroups);
				return;
			}
			if (listGroupIds.get_Item(0) == 0uL)
			{
				this.cbPrivate.IsChecked = true;
				this.uncheckdAllExcept(this.cbPrivate, this.panelGeneralSet);
				this.uncheckdAllExcept(this.cbPrivate, this.panelCustomGroups);
				return;
			}
			if (listGroupIds.get_Item(0) >= 6uL)
			{
				using (List<ulong>.Enumerator enumerator = listGroupIds.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						ulong current = enumerator.get_Current();
						using (IEnumerator<UIElement> enumerator2 = this.panelCustomGroups.get_Children().GetEnumerator())
						{
							while (enumerator2.MoveNext())
							{
								UIElement current2 = enumerator2.get_Current();
								SnsPostVisiItem snsPostVisiItem = current2 as SnsPostVisiItem;
								if (snsPostVisiItem != null && snsPostVisiItem.mSnsGroup != null && snsPostVisiItem.mSnsGroup.nTagID == current)
								{
									snsPostVisiItem.IsChecked = true;
								}
							}
						}
					}
				}
			}
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/Plugin/Sns/Common/UI/UploadAndPost/SetPostVisibilityPage.xaml", 2));
			this.LayoutRoot = (Grid)base.FindName("LayoutRoot");
			this.panelGeneralSet = (StackPanel)base.FindName("panelGeneralSet");
			this.cbInGroup = (SnsPostVisiItem)base.FindName("cbInGroup");
			this.cbPublic = (SnsPostVisiItem)base.FindName("cbPublic");
			this.cbPrivate = (SnsPostVisiItem)base.FindName("cbPrivate");
			this.borderLineCGroup = (Border)base.FindName("borderLineCGroup");
			this.panelCustomGroups = (StackPanel)base.FindName("panelCustomGroups");
			this.panelBtns = (StackPanel)base.FindName("panelBtns");
			this.btnEditGroup = (ClickableLayout)base.FindName("btnEditGroup");
			this.btnSetOutGroup = (ClickableLayout)base.FindName("btnSetOutGroup");
		}
	}
}
